﻿/*
 *该JS供一级页面 signup.html使用
 不同于同目录下的其他JS
 */
//控件合法性验证
var validationRules = {
    BnkAllow: 0,
    Regex: 1,
    SameAs: 2
};
var validCtrlItem = {
    ctrlData: [{
        id: 'stuext_userName',
        type: validationRules.Regex,
        reg: /^[0-9a-z]{6,12}$/
    }, {
        id: 'stuext_password',
        type: validationRules.Regex,
        reg: /^[0-9a-z]{6,12}$/
    }, {
        id: 'password_2',
        type: validationRules.SameAs,
        fowCtrl: 'stuext_password'
    }, {
        id: 'stubs_name',
        type: validationRules.Regex,
        reg: /^\S{2,}$/
    }, {
        id: 'stubs_regHome',
        type: validationRules.Regex,
        reg: /^\S{8,}$/
    }, {
        id: 'stubs_idCard',
        type: validationRules.Regex,
        reg: /^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})([0-9xX])$/
    }, {
        id: 'stubs_birthday',
        type: validationRules.Regex,
        reg: /\d{4}\-\d{2}\-\d{2}/
    }, {
        id: 'inRelation1',
        type: validationRules.BnkAllow
    }, {
        id: 'inFMember1',
        type: validationRules.BnkAllow
    }, {
        id: 'inFMemberContact1',
        type: validationRules.BnkAllow
    }, {
        id: 'stubs_partyJoinTime',
        type: validationRules.Regex,
        reg: /\d{4}\-\d{2}\-\d{2}/
    }, {
        id: 'stubs_partyTransferTime',
        type: validationRules.Regex,
        reg: /\d{4}\-\d{2}\-\d{2}/
    }, {
        id: 'stubs_contactAddress',
        type: validationRules.BnkAllow
    }, {
        id: 'stubs_phoneNumber',
        type: validationRules.BnkAllow
    }, {
        id: 'stubs_postCode',
        type: validationRules.Regex,
        reg: /\d+/
    }, {
        id: 'stubs_email',
        type: validationRules.Regex,
        reg: /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
    }, {
        id: 'stuapl_fileBlgOrg',
        type: validationRules.BnkAllow
    }, {
        id: 'stuapl_fileBlgOrgAddress',
        type: validationRules.BnkAllow
    }, {
        id: 'stuap1_fileBlgPostCode',
        type: validationRules.Regex,
        reg: /\d+/
    }, {
        id: 'stuedu_unGrdtTime',
        type: validationRules.Regex,
        reg: /\d{4}\-\d{2}\-\d{2}/
    }, {
        id: 'stuedu_bachelorTime',
        type: validationRules.Regex,
        reg: /\d{4}\-\d{2}\-\d{2}/
    }, {
        id: 'stuedu_postGrdtTime',
        type: validationRules.Regex,
        reg: /\d{4}\-\d{2}\-\d{2}/
    }, {
        id: 'stuedu_moDTime',
        type: validationRules.Regex,
        reg: /\d{4}\-\d{2}\-\d{2}/
    }, {
        id: 'stuext_peopleCount',
        type: validationRules.Regex,
        reg: /\d+/
    }, {
        id: 'stuext_rankInMajor',
        type: validationRules.Regex,
        reg: /\d+/
    }]
}
/*
 *
 *
 */
function soapWrapFunc(bdStr){
    var soapHead = '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/"><env:Body>', soapTail = '</env:Body></env:Envelope>';
    return soapHead + bdStr + soapTail;
}

function loadUniCodeById(id, uniProObj){
    var uniFunc = function(xml){
        var universityString = '';
        $.each($(xml).find('item'), function(i, n){
            universityString += '<option value="' + $(this).find('unicd_id').text() + '">' + $(this).find('unicd_unysyName').text() + '</option>';
        });
        $('#' + uniProObj.attr('valCtrlID')).html(universityString);
    }
    var sendstr = '<web:GetAllUniversityCodeInfoById><id>' + id + '</id></web:GetAllUniversityCodeInfoById>';
    ajaxDispatcher('UniversityCodeService', sendstr, uniFunc);
    
}

function loadCityCodeById(id){
    var cityFunc = function(xml){
        var selText = '';
        $.each($(xml).find('item'), function(i, n){
            selText += '<option value="' + $(this).find("ci_id").text() + '">' + $(this).find('ci_cityName').text() + '</option>';
        });
        $('#stubs_cityID').html(selText);
    }
    var sendstr = '<web:GetCityCodeById><id>' + id + '</id></web:GetCityCodeById>';
    ajaxDispatcher('CityCodeService', sendstr, cityFunc);
    
}

//代码表请求统一交付这个函数
function ajaxDispatcher(svcName, orderStr, funcObj){
    $.ajax({
        type: 'POST',
        contentType: 'text/xml',
        url: 'Service/' + svcName,
        data: soapWrapFunc(orderStr),
        dataType: 'xml',
        success: funcObj,
        beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
            if (x.responseText.indexOf('oops') >= 0) {
                alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                return;
            }
            else {
                alert('error:' + x.responseText);
            }
        }
    });
}

//加载代码表的函数
function loadProvinceCode(){
    var provFunc = function(xml){
        var selText = '';
        $.each($(xml).find('item'), function(i, n){
            selText += '<option value="' + $(this).find("pr_id").text() + '">' + $(this).find("pr_provName").text() + '</option>';
        });
        $("#province").html(selText).trigger('change');
        $(".pro").html(selText).trigger('change');
    }
    var sendstr = '<web:GetAllProvinceCodeInfo></web:GetAllProvinceCodeInfo>';
    
    ajaxDispatcher('ProvinceCodeService', sendstr, provFunc);
}

function loadDegreeTypeCode(){
    var degreeFunc = function(xml){
        var selText = '';
        $.each($(xml).find('item'), function(i, n){
            selText += '<option value="' + $(this).find("de_id").text() + '">' + $(this).find("de_degreeType").text() + '</option>';
        });
        $('#stuedu_bachelorType').html(selText);
        $('#stuedu_moDType').html(selText);
    }
    var sendstr = '<web:GetAllDegreeTypeCodeInfo></web:GetAllDegreeTypeCodeInfo>';
    
    ajaxDispatcher('DegreeTypeCodeService', sendstr, degreeFunc);
}

function loadMajorCode(){
    var majorFunc = function(xml){
        var selText = '';
        $.each($(xml).find('item'), function(i, n){
            selText += '<option value="' + $(this).find("ma_id").text() + '">' + $(this).find("ma_majorName").text() + '</option>';
        });
        $("#stubs_majorID").html(selText);
    }
    var sendstr = '<web:GetAllMajorInfo></web:GetAllMajorInfo>';
    
    ajaxDispatcher('MajorCodeService', sendstr, majorFunc);
}

//加载配置文件
function loadConfig(){
    var configFunc = function(xml){
        $.each($(xml).find("return"), function(i, n){
            $('#stuext_theYear').val($(this).find("year").text());
            $('#stuext_theSeason').val($(this).find("season").text());
        });
    }
    var sendstr = '<web:readConfig></web:readConfig>';
    
    ajaxDispatcher('ConfigService', sendstr, configFunc);
}

//批量绑定控件合法性验证
function attachCtrlValidation(){
    $.each(validCtrlItem.ctrlData, function(i, n){
    
        var ctrlObj = $('#' + n.id), slot = n, itsLine = ctrlObj.parents('.singleField');
        ctrlObj.attr('code', '0').focusin(function(){
            itsLine.find('.tip').show();
        }).focusout(function(){
            var ctrlVal = $.trim($(this).val()), selfValid = true;
            
            itsLine.find('.tip').hide();
            //是正则验证
            if (slot.type == validationRules.Regex) {
                if (!slot.reg.test(ctrlVal)) {
                    selfValid = false;
                }
            }
            //不允许空
            else {
                if (slot.type == validationRules.BnkAllow) {
                    if (ctrlVal == '') {
                        selfValid = false;
                    }
                }
                else {
                    //要和某控件值相同
                    if (slot.type == validationRules.SameAs) {
                        if (ctrlVal != $.trim($('#' + slot.fowCtrl).val())) {
                            selfValid = false;
                        }
                    }
                }
            }
            if (!selfValid) {
                $(this).attr('code', '0');
                itsLine.addClass('invalidField').find('.tip').show();
            }
            else {
                $(this).attr('code', '1');
                itsLine.removeClass('invalidField');
            }
        });
    });
}

$(function(){

});
/*
 */
$(window).load(function(){

    loadConfig();
    //3张代码表
    loadMajorCode();
    loadProvinceCode();
    loadDegreeTypeCode();
    
    $('#province').change(function(){
        var index = $("#province").val();
        loadCityCodeById(index);
    });
    
    //二级联动获得该省院校
    $('.pro').change(function(){
        var index = $(this).val();
        loadUniCodeById(index, $(this));
    });
    //下一步
    $('.stepBoard .nextButton').click(function(){
        var stepBord = $(this).parents('.stepBoard');
        stepBord.find('input:visible').trigger('focusin').trigger('focusout');
        var invalidInput = stepBord.find('input[code="0"]:visible');
        if (invalidInput.length > 0) {
            alert('请检查表单');
            invalidInput.parents('.singleField').addClass('invalidField').find('.tip').show();
            ;
        }
        else {
            $('.stepBoard').hide();
            stepBord.next('.stepBoard').show();
        }
    });
    //上一步
    $('.stepBoard .prButton').click(function(){
        $('.stepBoard').hide();
        $(this).parents('.stepBoard').prev('.stepBoard').show();
    });
    
    
    $('.timezone').datePicker({
        startDate: '1900-01-01',
        endDate: '2100-01-01',
        clickInput: true,
        verticalOffset: 35
    });
    //绑定正则验证
    attachCtrlValidation();
    //验证用户名是否存在
    $('#stuext_userName').focusout(function(){
        if ($('#stuext_userName').attr('code') == '0') {
            return false;
        }
        var uidstr = '<web:VerifyUserName><uname>' + $.trim($(this).val()) + '</uname></web:VerifyUserName>';
        $.ajax({
            type: "POST",
            contentType: "text/xml",
            url: 'Service/StudentExtendInfoService',
            data: soapWrapFunc(uidstr),
            dataType: 'xml',
            success: function(xml){
                if ($(xml).find('return').text() == '1') {
                    $('#usrno').hide();
                    $('#usryes').show();
                }
                else {
                    $('#stuext_userName').attr('code', '0');
                    $('#usrno').show();
                    $('#usryes').hide();
                }
            },
            beforeSend: function(xhr){
                xhr.setRequestHeader("SOAPAction", "\"\"");
            },
            error: function(x, e){
                alert('error:' + x.responseText);
            },
            complete: function(x){
            
            }
        });
    });
    
    //政治面貌
    $('#secPltAff').change(function(){
        if ($(this).val() == '3') {
            $('#PltAppendix').show();
        }
        else {
            $('#PltAppendix').hide();
        }
    });
    
    $('input[name="stuedu_unGrdt"]').click(function(){
        if ($(this).val() == '0') {
            $('#UnGrdtAppendix').hide();
        }
        else {
            $('#UnGrdtAppendix').show();
        }
    });
    $('input[name="stuedu_bachelorDegree"]').click(function(){
        if ($(this).val() == '0') {
            $('#BachelorAppendix').hide();
        }
        else {
            $('#BachelorAppendix').show();
        }
    });
    $('input[name="stuedu_postGrdt"]').click(function(){
        if ($(this).val() == '0') {
            $('#PostGrdtAppendix').hide();
        }
        else {
            $('#PostGrdtAppendix').show();
            if ($(this).val() == '1') {
                $('#PostGrdtAppendix .singleField label').filter('.shuo').show().end().filter('.bo').hide();
            }
            else {
                $('#PostGrdtAppendix .singleField label').filter('.bo').show().end().filter('.shuo').hide();
            }
        }
    });
    $('input[name="stuedu_moDDegree"]').click(function(){
        if ($(this).val() == '0') {
            $('#ModAppendix').hide();
        }
        else {
            $('#ModAppendix').show();
            if ($(this).val() == '1') {
                $('#ModAppendix .singleField label').filter('.shuo').show().end().filter('.bo').hide();
            }
            else {
                $('#ModAppendix .singleField label').filter('.bo').show().end().filter('.shuo').hide();
            }
        }
    });
    //提交注册信息
    $('#btnSubmitInfo').click(function(){
        //处理婚姻情况的函数
        var marriedFunc = function(){
            return $('#stubs_isMarried').attr("checked") == "checked" ? 1 : 0;
        }
        //拼合家庭成员JOSN的函数
        var familyFunc = function(){
            var stubs_family = '{"mem":[';
            var farela1 = $.trim($('#inRelation1').val()), faname1 = $.trim($('#inFMember1').val()), facon1 = $.trim($('#inFMemberContact1').val());
            if (farela1 != '' && faname1 != '' && facon1 != '') {
                stubs_family += '{"rel":"' + farela1 + '","name":"' + faname1 + '","con":"' + facon1 + '"}';
            }
            var farela2 = $.trim($('#inRelation2').val()), faname2 = $.trim($('#inFMember2').val()), facon2 = $.trim($('#inFMemberContact2').val());
            if (farela2 != '' && faname2 != '' && facon2 != '') {
                stubs_family += ',{"rel":"' + farela2 + '","name":"' + faname2 + '","con":"' + facon2 + '"}';
            }
            stubs_family += ']}';
            return stubs_family;
        }
        //发送节点设置
        var sendItems = {
            baseEntry: ['stubs_name', {
                node: 'stubs_sex',
                check: 'raSex'
            }, 'stubs_majorID', 'stubs_regHome', {
                node: 'stubs_isMarried',
                func: marriedFunc
            }, 'stubs_idCard', 'stubs_birthday', 'stubs_raceID', {
                node: 'stubs_family',
                func: familyFunc
            }, 'stubs_pltAff', 'stubs_partyJoinTime', 'stubs_partyTransferTime', 'stubs_cityID', 'stubs_contactAddress', 'stubs_postCode', 'stubs_phoneNumber', 'stubs_email', 'stubs_IMNumber'],
            applyEntry: ['stuapl_experience', 'stuapl_organization', 'stuapl_fileBlgOrg', 'stuapl_fileBlgOrgAddress', 'stuap1_fileBlgPostCode', 'stuapl_applyDivision', 'stuapl_applyMajor', 'stuapl_sourceType', 'stuapl_note'],
            eduEntry: [{
                node: 'stuedu_unGrdt',
                check: 'stuedu_unGrdt'
            }, 'stuedu_unGrdtSchool', 'stuedu_unGrdtTime', 'stuedu_unGrdtMajor', 'stuedu_unGrdtNumber', {
                node: 'stuedu_bachelorDegree',
                check: 'stuedu_bachelorDegree'
            }, 'stuedu_bachelorTime', 'stuedu_bachelorType', 'stuedu_bachelorSchool', 'stuedu_bachelorMajor', 'stuedu_bachelorNumber', {
                node: 'stuedu_postGrdt',
                check: 'stuedu_postGrdt'
            }, 'stuedu_postGrdtSchool', 'stuedu_postGrdtTime', 'stuedu_postGrdtMajor', 'stuedu_postGrdtNumber', {
                node: 'stuedu_moDDegree',
                check: 'stuedu_moDDegree'
            }, 'stuedu_moDTime', 'stuedu_moDType', 'stuedu_moDSchool', 'stuedu_moDMajor', 'stuedu_moDNumber', 'stuedu_awards'],
            extEntry: ['stuext_userName', 'stuext_password', 'stuext_examType', 'stuext_universityBatch', 'stuext_rankInMajor', 'stuext_peopleCount', 'stuext_talents', 'stuext_learnMeanType', 'stuext_techTitle', 'stuext_adminJob', 'stuext_examPlace', 'stuext_detailAddress', 'stuext_theYear', 'stuext_theSeason']
        };
        
        var sendBody = '<web:NewStudentInfo>';
        $.each(sendItems, function(i, n){
            sendBody += '<' + i + '>';
            //遍历分数组
            for (var j = 0; j < n.length; j++) {
                var oo = n[j];
                
                if (typeof(oo) == 'string') {
                    sendBody += '<' + oo + '>' + $.trim($('#' + oo).val()) + '</' + oo + '>';
                    if (oo == 'stubs_pltAff' && $.trim($('#stubs_pltAff').val()) != '3') {
                        j += 2;
                        continue;
                    }
                }
                else {
                    if (oo.func != undefined) {
                        sendBody += '<' + oo.node + '>' + oo.func() + '</' + oo.node + '>';
                    }
                    else {
                        sendBody += '<' + oo.node + '>' + $('input[name=' + oo.check + ']:checked').val() + '</' + oo.node + '>';
                        if ((oo.node == 'stuedu_unGrdt' || oo.node == 'stuedu_postGrdt') && $('input[name=' + oo.check + ']:checked').val() == '0') {
                            j += 4;
                            continue;
                        }
                        if ((oo.node == 'stuedu_bachelorDegree' || oo.node == 'stuedu_moDDegree') && $('input[name=' + oo.check + ']:checked').val() == '0') {
                            j += 5;
                            continue;
                        }
                    }
                }
            }
            sendBody += '</' + i + '>';
        });
        sendBody += '</web:NewStudentInfo>';
        
        $.ajax({
            type: "POST",
            contentType: "text/xml",
            url: 'Service/StudentService',
            data: soapWrapFunc(sendBody),
            dataType: 'xml',
            success: function(xml){
                alert($(xml).find('return').text());
                
            },
            beforeSend: function(xhr){
                xhr.setRequestHeader("SOAPAction", "\"\"");
            },
            error: function(x, e){
                alert('error:' + x.responseText);
            },
            complete: function(x){
            
            }
        });
    });
});
